<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>发布商品</title>

    <!-- Bootstrap -->
    <link href="/views/client/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="../js/html5shiv.min.js"></script>
    <script src="../js/respond.min.js"></script>
    <![endif]-->
    <script src="/views/dist/js/vue.js"></script>
    <script src="/views/dist/js/axios.js"></script>
    <link rel="stylesheet" href="/views/client/css/common.css">
    <script src="/views/client/js/common.js"></script>

    <script src="/views/client/js/tinymce.js" referrerpolicy="origin"></script>

    <style>
        .container {
            background-color: #fff;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div id="app">
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <a class="navbar-brand" href="/views/client/index.html">MALL</a>
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                        data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">MALL</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <!--                <li class=""><a href="#">Link <span class="sr-only">(current)</span></a></li>-->
                    <!--                <li><a href="#">Link</a></li>-->

                </ul>
                <form class="navbar-form navbar-left">
                </form>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="./register.html" v-if="currentUser.id===undefined">注册</a></li>
                    <li><a href="./login.html" v-if="currentUser.id===undefined">登录</a></li>
                    <li class="dropdown" v-if="currentUser.id!==undefined">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                           aria-expanded="false">
                            <img :src="currentUser.avatar" class=" img-circle" style="width: 30px;height: 30px;" alt="">
                            {{currentUser.username}}
                            <span class="caret"></span></a>

                        <ul class="dropdown-menu" v-if="currentUser.role===3">
                            <li><a href="./info.html">我的信息</a></li>
                            <li><a href="/views/client/store/publishGoods.html">发布商品</a></li>
                            <li><a href="/views/client/store/myGoods.html">我发布的商品</a></li>
                            <li><a href="/views/client/store/orders.html">我收到的订单</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a @click="this.clientLogout()" href="#">退出</a></li>
                        </ul>
                    </li>
                </ul>
            </div><!-- /.navbar-collapse -->
        </div><!-- /.container-fluid -->
    </nav>
    <div class="container">
        <ul class="nav nav-pills" style="margin:10px 0;">
            <li role="presentation" class=""><a href="./info.html">我的信息</a></li>
            <li class="active"><a href="/views/client/store/publishGoods.html">发布商品</a></li>
            <li role="presentation"><a href="/views/client/store/myGoods.html">我发布的商品</a></li>
            <li role="presentation" class=""><a href="/views/client/store/orders.html">我的订单</a></li>
        </ul>

        <h3 class="modal-title">发布商品</h3>
        <div class="goodsInfo">
            <div class="form-group">
                <label for="name">商品名称</label>
                <input id="name" placeholder="商品名称" type="text" name="name" class="form-control"
                       v-model.trim-="newGoods.name">
            </div>
            <div class="form-group">
                <label for="categoryId">类别</label>
                <select id="categoryId" name="categoryId" class="form-control" v-model="newGoods.categoryId">
                    <option selected disabled value="-1">请选择商品类别</option>
                    <option :value="catrgory.id" v-for="catrgory of categories">{{catrgory.name}}</option>
                </select>
            </div>
            <!--            //商家id由服务端解析token获得-->
            <div class="form-group">
                <label for="keywords">商品关键字</label>
                <input id="keywords" name="keywords" placeholder="商品关键字，以逗号隔开" type="text" class="form-control"
                       v-model.trim="newGoods.keywords"/>
            </div>

            <div class="form-group">
                <label for="brief">商品简介</label>
                <input style="height: 60px;" placeholder="请输入商品简介" type="text" name="brief" id="brief"
                       class="form-control" v-model.trim="newGoods.brief"/>
            </div>
            <div class="form-group">
                <label for="price">商品单价</label>
                <input type="number" placeholder="请输入商品单价" name="price" class="form-control" id="price"
                       v-model="newGoods.price"/>
            </div>
            <div class="form-group">
                <label for="primaryImage">请选择商品主页显示的图片</label>
                <div class="avatar">
                    <img :src="newGoods.primaryImage" alt="" class="img-thumbnail" style="width: 140px;height: 140px;">
                    <input id="primaryImage" multiple @change="uploadPrimaryImage" class="btn"
                           style="width: 140px;height: 140px;display: inline-block;border-radius: 4px;border:1px solid black;"
                           type="file"/>
                </div>
                <div class="form-group">
                    <label for="mytextarea"></label>
                    <textarea id="mytextarea" placeholder="商品详细描述" name="detail" cols="30" rows="10"
                              v-model="newGoods.detail">{{newGoods.detail}}</textarea>
                </div>
                <div class="form-group">
                    <button style="height: 45px;" class="form-control btn btn-success" type="button"
                            @click="uploadGoods()">
                        提交
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
<footer class="main-footer">
    <strong>Copyright &copy; 2020 <a href="">MALL</a></strong>
    All rights reserved.
    <div class="float-right d-none d-sm-inline-block">
        <b>Version</b> v1.0.0
    </div>
</footer>

</body>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="/views/client/js/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="/views/client/js/bootstrap.min.js"></script>
<script src="/views/client/js/cookie.js"></script>
<!--int@daffodilvarsity.edu.bd-->
<script>
    tinymce.init({
        selector: '#mytextarea',
        height: 700,
        plugins: [
            'advlist autolink lists link image charmap print preview anchor',
            'searchreplace visualblocks code fullscreen',
            'insertdatetime media table paste imagetools wordcount'
        ],
        toolbar: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
        content_style: 'body { font-family:Helvetica,Arial,sans-serif; font-size:14px }',
        images_upload_url: '/common/file/image',
        images_reuse_filename: true,
        images_upload_base_path: 'https://mall-1300780430.cos.ap-nanjing.myqcloud.com/',
    }).then(content => {
        tinymceContent = content[0];
    });

</script>
<script>
    var qCloudBaseUrl = qCloudBaseUrl;
    var app = new Vue({
        el: "#app",
        data: {
            currentUser: {},
            newGoods: {
                categoryId: -1,
                primaryImage: ""
            },
            categories: []
        },
        methods: {
            uploadGoods() {
                console.log(this.newGoods);
                if (this.newGoods.name === undefined || this.newGoods.name.trim() === "") {
                    alert("商品名称不应为空！");
                    return;
                }
                if (this.newGoods.categoryId === -1) {
                    alert("商品类别不应为空！");
                    return;
                }
                if (this.newGoods.keywords === "") {
                }
                if (this.newGoods.price === undefined) {
                    alert("请输入商品单价！");
                    return;
                }
                this.newGoods.detail = tinymceContent.getContent();
                axios.post("/client/goods/goods", {
                    id: this.newGoods.id,
                    name: this.newGoods.name,
                    categoryId: this.newGoods.categoryId,
                    keywords: this.newGoods.keywords,
                    brief: this.newGoods.brief,
                    price: this.newGoods.price,
                    detail: this.newGoods.detail,
                    primaryImage: this.newGoods.primaryImage
                }, {
                    headers: {
                        Authorization: localStorage.getItem("accessToken") != null ? "Bearer " + localStorage.getItem("accessToken") : ""
                    }
                })
                    .then(response => {
                        console.log(response.data);
                        if (response.data.code === 200) {
                            this.newGoods = response.data.data;
                            alert(response.data.message);
                        }
                    }).catch(err => {
                    console.log(err);
                })
            },
            uploadPrimaryImage(event) {
                const file = event.target.files[0];
                var forms = new FormData();
                forms.append('file', file);
                //10 -> primaryImage
                forms.append('type', 10);
                forms.append('locationPrefix', '/images/')
                axios.post("/common/file/image", forms, {
                    headers: {
                        'Content-type': 'multipart/form-data',
                        Authorization: localStorage.getItem("accessToken") != null ? "Bearer " + localStorage.getItem("accessToken") : ""
                    }
                }).then(response => {
                    console.log(response.data);
                    this.newGoods.primaryImage = qCloudBaseUrl + response.data.location;
                }).catch(err => {
                    console.log(err);
                })
            },
        },
        mounted: function () {
            retrieveUserByToken();
            retrieveAllCategories();
            const goodsId = retrieveQueryVariable("goodsId");
            console.log(goodsId);
            if (goodsId !== undefined) {
                retrieveGoodsById(goodsId);
            }
        }
    });
</script>

</html>